--- title: "L2-026 小字辈" created: 2025-11-28 tags: - 算法 --- # L2-026 小字辈 ## 题目 [L2-026 小字辈](https://pintia.cn/problem-sets/994805046380707840/exam/problems/type/7?problemSetProblemId=994805055679479808&page=1) ![[image-8ac5a29c.png]] ## 思路分析 ## 代码实现 ```cpp #include using namespace std; #define endl '\n' using ll = long long; using ull = unsigned long long; using PII = pair; using Pll = pair; int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1}; const int inf = 0x3f3f3f3f; priority_queue pq; multiset s; vector> g; int maxDeep=-inf; vector ans; void dfs(int u,int deep){ if(deep>=maxDeep){ if(deep>maxDeep){ ans.clear(); maxDeep=deep; ans.push_back(u); } else if(deep==maxDeep){ ans.push_back(u); } } for(auto nx:g[u]){ dfs(nx,deep+1); } } int main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int n;cin>>n; g.resize(n+1); vector roots; for(int i=1;i<=n;i++){ int fa;cin>>fa; if(fa==-1) roots.push_back(i); else g[fa].push_back(i); } for(auto r:roots) dfs(r, 1); cout<